package cn.net.shuxing.yifu.common.service;

import cn.net.shuxing.yifu.common.bean.ShareholderLevelList;
import cn.net.shuxing.yifu.common.bean.UserCapital;
import cn.net.shuxing.yifu.common.constant.UserType;
import cn.net.shuxing.yifu.common.mapper.ShareholderLevelListMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import java.math.BigDecimal;
import java.util.List;

@Service
public class ShareholderLevelListService extends BaseService<ShareholderLevelList, ShareholderLevelListMapper> {

    @Autowired
    private UserCapitalService userCapitalService;

    @Autowired
    private UserRoleService userRoleService;

    public int insert(int coinTypeId, int level, BigDecimal low, BigDecimal high, String desc) {
        ShareholderLevelList memberLevel = new ShareholderLevelList();
        memberLevel.setLevel(level);
        memberLevel.setCoinTypeId(coinTypeId);
        memberLevel.setLow(low);
        memberLevel.setHigh(high);
        memberLevel.setDescription(desc);
        return insert(memberLevel);
    }

    @Override
    public int insert(ShareholderLevelList bean) {
        Integer level = bean.getLevel();
        if (level <= 1) {
            bean.setLevel(1);
            bean.setLow(new BigDecimal(0));
        } else {
            ShareholderLevelList memberLevel = getByCoinTypeAndLevel(bean.getCoinTypeId(), bean.getLevel() - 1);
            if (memberLevel == null)
                return -1;
            bean.setLow(memberLevel.getHigh());
        }
        return super.insert(bean);
    }


    public ShareholderLevelList getByCoinTypeAndLevel(Integer coinTypeId, Integer level) {
        ShareholderLevelList memberLevel = new ShareholderLevelList();
        memberLevel.clearCreateTimeAndUpdateTime();
        memberLevel.setLevel(level);
        memberLevel.setCoinTypeId(coinTypeId);
        return get(memberLevel);
    }

    public int calcaulateShareholderLevel(Integer userId) {
        List<UserCapital> userCapitals = userCapitalService.getsByUserId(userId);
        BigDecimal totalSleepCoinCount = new BigDecimal(0);
        if (userCapitals != null && !userCapitals.isEmpty()) {
            for (UserCapital userCapital : userCapitals) {
                BigDecimal sleepCoinCount = userCapital.getSleepCoinCount();
                totalSleepCoinCount= totalSleepCoinCount.add(sleepCoinCount);
            }
        }

        Example example = new Example(ShareholderLevelList.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andLessThan("low", totalSleepCoinCount);
        criteria.andGreaterThanOrEqualTo("high", totalSleepCoinCount);
        ShareholderLevelList memberLevel = getByExample(example);
        if (memberLevel == null)
            return -1;
        return memberLevel.getLevel();
    }

    public void calculateShareholderLevelCount() {
        List<Integer> integers = userRoleService.getsByUserRole(UserType.MEMBER);
        List<ShareholderLevelList> all = getAll();
        for (ShareholderLevelList memberLevel : all) {
            Integer level = memberLevel.getLevel();
            Integer newCount = 0;
            if (integers != null && !integers.isEmpty()) {
                for (Integer integer : integers) {
                    int i = calcaulateShareholderLevel(integer);
                    if (level == i) {
                        newCount += 1;
                    }
                }
            }
            memberLevel.setCount(newCount);
            update(memberLevel);
        }
    }

    public List<ShareholderLevelList> getAllLevel() {
        List<ShareholderLevelList> all = getAll();
        return all;
    }
}
